<!-- 礼包管理 -->
<template>
  <el-main class="base_main_content">
    <div class="base_screen_header">
      <el-button type="primary" @click="add">新增礼包</el-button>
      <el-form :inline="true" class="base_search_form" @submit.prevent>
        <el-form-item>
          <el-input
            v-model="name"
            @keyup[native].enter="searchName"
            @change="searchName"
            placeholder="礼包名称"
            clearable
            :suffix-icon="Search"
          />
        </el-form-item>
      </el-form>
    </div>
    <StatusBar :data="statusList" default="all" />
    <div class="base_main_header">
      <el-button type="primary" text @click="giftExport">导出</el-button>
    </div>
    <GiftTable route="giftmanager" @getNum="getNum" />
  </el-main>
</template>

<script setup>
import StatusBar from '@com/StatusBar.vue';
import GiftTable from "@com/marketing/GiftTable.vue";
import { ref, reactive } from 'vue'
import api from "@api/gift"
import { Search } from '@element-plus/icons-vue'
import $ from "@util/base"
import useRoute from "@util/router";
const route = useRoute()

const name = ref(route.query().name || '')

const statusList = reactive([{
  label: '全部',
  value: 'all',
  total: 0
}, {
  label: '已上架',
  value: '1004003',
  total: 0
}, {
  label: '未上架',
  value: '1004002',
  total: 0
}])

getNum();
function getNum() {
  api.num((val) => {
    numData(val);
  });
}

function numData(val) {
  statusList.forEach((e, i) => {
    e.total = val[i];
  });
}

function searchName(val) {
  route.replaceQuery('name', val)
}

function add() {
  route.push({ name: 'giftdetails', query: { type: 'add' } })
}

function giftExport() {
  let routeQuery = route.query()
  api.export({
    name: routeQuery.name,
    status: routeQuery.status,
  }, val => {
    $.download(val.url, "礼包导出")
  })
}

</script>